﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.Remoting;
using DefectTracker.Business;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Configuration;

namespace DefectTracker.Service
{
    partial class SecurityHost : ServiceBase
    {
        TcpServerChannel m_TcpServerChannel;

        public SecurityHost()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            int securityPort;
            if (!int.TryParse(ConfigurationManager.AppSettings["SecurityPort"], out securityPort) || securityPort <= 0)
                throw new ConfigurationErrorsException("Configuration setting \"SecuirytPort\" not found or is not valid in \"App.Config\"");
            m_TcpServerChannel = new TcpServerChannel(securityPort);
            ChannelServices.RegisterChannel(m_TcpServerChannel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(BugTrackerSecurity), "Security", WellKnownObjectMode.Singleton);
        }

        public void Start()
        {
            OnStart(null);
        }

        protected override void OnStop()
        {
            ChannelServices.UnregisterChannel(m_TcpServerChannel);
        }
    }
}
